#
# Copyright 2010 Steve Leach
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#

wspace = {}
w_systems = {}

def load_wspace():
	# Parse the wormhole info file
	f = open('data/wspace.txt')
	for line in f:
		if line.strip():
			# Columns 1-9 are region & constellation
			key = line[:10].rstrip()
			if not key == 'Reg Const':
				# WH info is from column 10 on
				value = line[10:].strip()
				wspace[key] = value
	f.close()

def get_wh_info(region, constellation):
	key = region+' '+constellation
	if wspace.has_key(key):
		return wspace[key]
	else:
		key = region+' *'
		if wspace.has_key(key):
			return wspace[key]
		else:
			return None

def parse_systems(fileName):
	print 'system_name,region_key,constellation_key,static_data'
	f = open(fileName)
	for line in f:
		pos = line.find('VALUES (')
		value_text = line[pos+8:]
		values = value_text.split(',')

		region_id = values[0]
		constellation_id = values[1]
		system_name = values[3][2:-1]

		if region_id.startswith('11'):
			region_key = 'R' + region_id[-2:]
			constellation_key = 'C' + constellation_id[-3:]

			wh_data = get_wh_info(region_key, constellation_key)

			print system_name+','+region_id+','+constellation_id+','+wh_data
	f.close()

def parse_systems2(fileName):
	print "region_id,constellation_id,system_id,system_name,security,x,y,z,space_type"
	count = 0
	f = open(fileName)
	for line in f:
		pos = line.find('VALUES (')
		value_text = line[pos+8:-4]
		values = value_text.split(',')


		region_id = values[0]
		constellation_id = values[1]
		system_id = values[2]
		system_name = values[3][2:-1]
		security = values[21]
		x = values[4]
		y = values[5]
		z = values[6]

		if region_id.startswith('11'):
			space_type = 'wspace'
			print region_id+','+constellation_id+','+system_id+','+system_name+','+security+','+x+','+y+','+z+','+space_type
			count += 1
		else:
			space_type = 'kspace'

		#if count > 10:
		#	break
	f.close()

if __name__ == '__main__':
	load_wspace()
	parse_systems2('data/mapsystems.sql')

